using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace ClientTools.Client.Library.Settings
{
    public class Profile
    {
        private string _profileName;

        [XmlAttribute("ProfileName")]
        public string ProfileName
        {
            get { return _profileName; }
            set { _profileName = value; }
        }
        private string _umbracoUserName;

        public string UmbracoUserName
        {
            get { return _umbracoUserName; }
            set { _umbracoUserName = value; }
        }
        private string _umbracoPassword;

        public string UmbracoPassword
        {
            get { return _umbracoPassword; }
            set { _umbracoPassword = value; }
        }
        private string _dbConnectionString;

        [XmlElement("DBConnectionString")]
        public string DbConnectionString
        {
            get { return _dbConnectionString; }
            set { _dbConnectionString = value; }
        }

        private string _serviceFolderUrl;

        public string ServiceFolderUrl
        {
            get { return _serviceFolderUrl; }
            set { _serviceFolderUrl = value; }
        }

        //private string _packagerServiceUrl;

        //public string PackagerServiceUrl
        //{
        //    get { return _packagerServiceUrl; }
        //    set { _packagerServiceUrl = value; }
        //}

        private string _providerName;
        public string ProviderName
        {
            get { return _providerName; }
            set { _providerName = value; _providerObject = null; }
        }

        public void InitProvider() 
        {
            Library.Provider.ProviderInfo pi = Library.Provider.ProviderManager.ProviderInfos.GetProviderByName(_providerName);
            if (pi != null)
            {
                _providerObject = (Library.Interfaces.IProvider)Activator.CreateInstance(pi.ProviderType);
            }
            switch (_providerObject.GetType().Name)
            {
                case "WebserviceProvider":
                    Library.Provider.WebserviceProvider wsp = (Library.Provider.WebserviceProvider) _providerObject;
                    wsp.ServiceFolderUrl = this.ServiceFolderUrl;
                    wsp.UserName = this.UmbracoUserName;
                    wsp.Password = this.UmbracoPassword;
                    break;
                case "DatabaseProvider":
                    Library.Provider.DatabaseProvider dbp = (Library.Provider.DatabaseProvider) _providerObject;
                    dbp.ConnectionString = this.DbConnectionString;
                    break;
            }
        }

        private Library.Interfaces.IProvider _providerObject;
        [XmlIgnore] 
        public Library.Interfaces.IProvider ProviderObject 
        {
            get {
                if (_providerObject == null)
                {
                    Library.Provider.ProviderInfo pi = Library.Provider.ProviderManager.ProviderInfos.GetProviderByName(_providerName);
                    if (pi != null)
                    {
                        _providerObject = (Library.Interfaces.IProvider)Activator.CreateInstance(pi.ProviderType);
                    }
                }
                return _providerObject; 
            }
            set { 
                _providerObject = value;
                _providerName = _providerObject.Providername;
            }
        }
    }
}
